﻿import * as atypes from '../actionTypes';
import Candidate from './Candidate';
import {
    buildParent, reverse, reverseChecked, expand,
    DEPARTMENTS, SECTIONS, TEACHERS
} from '../../common/immutable';





function rebuildCandidates(college) {
    var candidates = [],
        addTeachers = (teachers, dep, sec) => {
            teachers && teachers.forEach(tea => {
                if (tea.checked) candidates.push(new Candidate(dep, sec, tea));
            })
        }
    if (college.checked)
        candidates.push(new Candidate());
    else {
        addTeachers(college.teachers);
        college.departments && college.departments.forEach(dep => {
            if (dep.checked)
                candidates.push(new Candidate(dep));
            else {
                addTeachers(dep.teachers, dep);
                dep.sections && dep.sections.forEach(sec => {
                    if (sec.checked) candidates.push(new Candidate(dep, sec));
                    else addTeachers(sec.teachers, dep, sec);
                })
            }
        })
    }
    return candidates;
}
function check(college, dep, sec, t) {
    if (t) {
        const nt = reverseChecked(t);
        if (!dep)   // check college teacher
            return buildParent(college, TEACHERS, t, nt);
        if (!sec) { // check department teacher
            const newDep = buildParent(dep, TEACHERS, t, nt);
            return buildParent(college, DEPARTMENTS, dep, newDep);
        }
        // check section teacher
        const newSec = buildParent(sec, TEACHERS, t, nt);
        const newDep = buildParent(dep, SECTIONS, sec, newSec);
        return buildParent(college, DEPARTMENTS, dep, newDep);
    } else {
        if (!dep)   // check college
            return reverseChecked(college);
        if (!sec)   // check department
            return buildParent(college, DEPARTMENTS, dep, reverseChecked(dep));
        // check section
        const newDep = buildParent(dep, SECTIONS, sec, reverseChecked(sec));
        return buildParent(college, DEPARTMENTS, dep, newDep);
    }
}
function removeCandidate(college, can) {
    return check(college, can.dep, can.sec, can.tea);
}
function clearCandidates(college, candidates) {
    if (candidates && candidates.length > 0) {
        // rebuild teachers
        // it's difficult to be implemented
    }
    return college;
}

// state = rootState.send
export default function receivers(state = null, action) {
    var college = null;
    switch (action.type) {
        case atypes.TOGGLE_PICKER:
            return reverse(state, 'pickerVisible');
        case atypes.TOGGLE_DEPARTMENT:
            return reverse(state, 'depVisible');
        case atypes.CHECK:
            college = check(state.college, action.department, action.section, action.teacher);
            break;
        case atypes.EXPAND:
            college = expand(state.college, action.department, action.section);
            break;
        case atypes.REMOVE_CANDIDATE:
            college = removeCandidate(state.college, action.candidate);
            break;
        case atypes.CLEAR_CANDIDATES:
            college = clearCandidates(state.college, state.candidates);
            break;
        default:
            return state;
    }
    const candidates = rebuildCandidates(college);
    return { ...state, candidates, college };
}

